package com.baizhi.controller;


import com.baizhi.entity.Phone;
import com.baizhi.service.IPhoneService;


import com.baizhi.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huiJie
 * @since 2021-10-21
 */
@RestController
@RequestMapping("/phone")
public class PhoneController {
    @Autowired
    private IPhoneService phoneService;
    @GetMapping
    public R showPagePhones(Integer pageNum, Integer pageSize){
        Page<Phone> phonePage = new Page<>(pageNum, pageSize);
        return R.ok(phoneService.page(phonePage));
    }
    @GetMapping("{id}")
    public R showPhoneById(@PathVariable("id")Integer id){
        return R.ok(phoneService.getById(id));
    }
    @PostMapping
    public R savePhone(@RequestBody Phone phone){
        try {
            phoneService.save(phone);
            return R.ok(null);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
    @PutMapping
    public R updatePhone(@RequestBody Phone phone){
        try {
            QueryWrapper<Phone> queryWrapper = new QueryWrapper<>();
            QueryWrapper<Phone> wrapper = queryWrapper.eq("phone_id", phone.getPhoneId());
            phoneService.update(phone, wrapper);
            return R.ok(null);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
    @DeleteMapping("{id}")
    public R removePhone(@PathVariable("id")Integer id){
        try {
            phoneService.removeById(id);
            return R.ok(null);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
}

